#pragma once
#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;

class Command
{
private:
    bool Is_Safe(const string& cmd)
    {
        for(auto& e:_white_exec)
        {
            if(cmd == e)
            {
                return true;
            }
        }
        return false;
    }
public:
    Command()
    {
        _white_exec.push_back("ls -a -l");
        _white_exec.push_back("ll");// 为什么一执行就错误
        _white_exec.push_back("cat test.txt");
        _white_exec.push_back("touch touch.txt");
        _white_exec.push_back("tree");
        _white_exec.push_back("whoami");
        _white_exec.push_back("who");
        _white_exec.push_back("pwd");
    }

    string Exec(const string &cmd)
    {
        if(!Is_Safe(cmd))
        {
            return "bad man";
        }
        string result;
        FILE* fp = popen(cmd.c_str(),"r");
        if(fp == nullptr)
        {
            result = cmd + " popen error";
        }
        else
        {
            char buffer[1024];
            while(fgets(buffer,sizeof(buffer),fp))
            {
                result += buffer;
            }
            pclose(fp);
        }
        return result;
    }

    ~Command()
    {
    }

private:
    vector<string> _white_exec;
};